{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/141_image_array_viz.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>\n",
    "\n",
    "**Visualizing in-memory raster datasets and image arrays**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !pip install -U geemap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "Use a Landsat image covering San Francisco Bay Area."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "m = geemap.Map()\n",
    "image = ee.Image(\"LANDSAT/LC08/C02/T1_TOA/LC08_044034_20140318\").select(\n",
    "    [\"B5\", \"B4\", \"B3\"]\n",
    ")\n",
    "vis_params = {\n",
    "    \"min\": 0.0,\n",
    "    \"max\": 0.4,\n",
    "}\n",
    "m.add_layer(image, vis_params, \"False Color (543)\")\n",
    "m.centerObject(image)\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "Retrieve the projection information from the image."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "projection = image.select(0).projection().getInfo()\n",
    "crs = projection[\"crs\"]\n",
    "crs"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7",
   "metadata": {},
   "source": [
    "Read the Earth Engine image into an Xarray DataArray."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "ds = geemap.ee_to_xarray(\n",
    "    image, crs=\"EPSG:32610\", scale=300, geometry=image.geometry(), ee_mask_value=0\n",
    ")\n",
    "ds"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9",
   "metadata": {},
   "source": [
    "Save the DataArray as a Cloud Optimized GeoTIFF."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.xee_to_image(ds, filenames=[\"landsat.tif\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11",
   "metadata": {},
   "source": [
    "Calculate the NDVI and save it to the DataArray."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "ndvi = (ds[\"B5\"] - ds[\"B4\"]) / (ds[\"B5\"] + ds[\"B4\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13",
   "metadata": {},
   "source": [
    "Create an in-memory raster dataset from the DataArray."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "ndvi_image = geemap.array_to_image(ndvi, source=\"landsat.tif\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15",
   "metadata": {},
   "source": [
    "Visualize the in-memory raster dataset."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16",
   "metadata": {},
   "outputs": [],
   "source": [
    "m.add_raster(\"landsat.tif\", indexes=[1, 2, 3], nodata=-1, layer_name=\"Landsat 7\")\n",
    "m.add_raster(ndvi_image, colormap=\"Greens\", layer_name=\"NDVI\")\n",
    "m"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17",
   "metadata": {},
   "source": [
    "Save the in-memory raster dataset to a GeoTIFF file."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.array_to_image(ndvi, output=\"ndvi.tif\", source=\"landsat.tif\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
